package business

import (
	"context"
	"group-buy/group_buy/internal/models"

	"group-buy/group_buy/internal/svc"
	"group-buy/group_buy/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type SaveBusinessLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewSaveBusinessLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SaveBusinessLogic {
	return &SaveBusinessLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *SaveBusinessLogic) SaveBusiness(req *types.Business) (resp *types.Business, err error) {
	m := &models.Business{
		Id:            req.Id,
		Name:          req.Name,
		ContactsName:  req.ContactsName,
		ContactsPhone: req.ContactsPhone,
		Address:       req.Address,
		WorkTime:      req.WorkTime,
		Preferential:  req.Preferential,
		Img:           req.Img,
		Sort:          req.Sort,
		UserId:        req.UserId,
		DealerId:      req.DealerId,
	}
	err = m.Save(l.svcCtx.Engine)
	if err != nil {
		return nil, err
	}
	req.Id = m.Id
	return req, nil
}
